// ==UserScript==
// @name					Google Image Search Context Menu
// @namespace				lkytal
// @description				Add Search Image context menu
// @author					lkytal
// @license					GPL
// @include					*
// @version					3.0.6
// @icon					http://lkytal.qiniudn.com/ic.ico
// @grant					GM_openInTab
// @homepageURL				https://git.oschina.net/coldfire/GM
// @updateURL				https://git.oschina.net/coldfire/GM/raw/master/meta/img.meta.js
// @downloadURL				https://git.oschina.net/coldfire/GM/raw/master/img.user.js
// ==/UserScript==

//if (!("contextMenu" in document.documentElement && "HTMLMenuItemElement" in window)) return;

var body = document.body;
body.addEventListener("contextmenu", initMenu, false);

var menu = body.appendChild(document.createElement("menu"));
menu.outerHTML = '<menu id="userscript-search-by-image" type="context">\
					<menuitem id="SearchGoogle" label="Search image via google"\
							  icon="">\
					</menuitem>\
					<menuitem id="SearchBaidu" label="Search image via Baidu"></menuitem>\
				</menu>';

document.querySelector("#SearchGoogle").addEventListener("click", searchImage, false);
document.querySelector("#SearchBaidu").addEventListener("click", search_baidu, false);

function initMenu(aEvent)
{
	var node = aEvent.target;
	var item = document.querySelectorAll("#userscript-search-by-image menuitem");

	if (node.localName == "img")
	{
		body.setAttribute("contextmenu", "userscript-search-by-image");

		for (var i = item.length - 1; i > -1; i--)
		{
			item[i].setAttribute("imageURL", node.src);
		}
	}
	else
	{
		body.removeAttribute("contextmenu");
		//item.removeAttribute("imageURL");
	}
}

function addParamsToForm(aForm, aKey, aValue)
{
	var hiddenField = document.createElement("input");
	hiddenField.setAttribute("type", "hidden");
	hiddenField.setAttribute("name", aKey);
	hiddenField.setAttribute("value", aValue);
	aForm.appendChild(hiddenField);
}

function searchImage(aEvent)
{
	// Executed when user click on menuitem
	// aEvent.target is the <menuitem> element
	var imageURL = aEvent.target.getAttribute("imageURL");

	if (imageURL.indexOf("data:") == 0)
	{
		var base64Offset = imageURL.indexOf(",");
		if (base64Offset != -1)
		{
			var inlineImage = imageURL.substring(base64Offset + 1).replace(/\+/g, "-").replace(/\//g, "_").replace(/\./g, "=");

			var form = document.createElement("form");
			form.setAttribute("method", "POST");
			form.setAttribute("action", "//www.google.com/searchbyimage/upload");
			form.setAttribute("enctype", "multipart/form-data");
			form.setAttribute("target", "_blank");
			addParamsToForm(form, "image_content", inlineImage);
			addParamsToForm(form, "filename", "");
			addParamsToForm(form, "image_url", "");
			body.appendChild(form);
			form.submit();
		}
	}
	else
	{
		GM_openInTab("//www.google.com/searchbyimage?image_url=" + encodeURIComponent(imageURL));
	}
}

function search_baidu(aEvent)
{
	var imageURL = aEvent.target.getAttribute("imageURL");

	GM_openInTab("//image.baidu.com/n/pc_search?queryImageUrl=" + encodeURIComponent(imageURL) + "&uptype=urlsearch");
}
